/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

using Microsoft.VisualStudio.Modeling.Diagrams;

namespace DslEditorPowerToy.Languages.EditorConfiguration
{
    internal static class DrawingHelper
    {
        #region Public Methods
        /// <summary>
        /// Adds a background banner to shape
        /// </summary>
        public static void AddBackgroundImageBanner(IList<ShapeField> shapeFields, Bitmap image)
        {

            //Add a Text ShapeField to background
            ImageField banner = new ImageField("Background", image);
            banner.DefaultFocusable = false;
            banner.DefaultSelectable = false;
            banner.DefaultVisibility = true;

            shapeFields.Add(banner);

            banner.AnchoringBehavior.SetTopAnchor(AnchoringBehavior.Edge.Top, 3.0);
            banner.AnchoringBehavior.SetLeftAnchor(AnchoringBehavior.Edge.Left, 0.01);
            //banner.AnchoringBehavior.SetRightAnchor(AnchoringBehavior.Edge.Right, 0.01);
            //banner.AnchoringBehavior.SetBottomAnchor(AnchoringBehavior.Edge.Bottom, 0.01);
        }
        /// <summary>
        /// Updates the outline style of the shape
        /// </summary>
        public static void UpdateOutlineStyle(NodeShape shape, bool update, DashStyle updateStyle, Color updateColor)
        {
            if (true == update)
            {
                //Change outline style
                PenSettings outlinePen = new PenSettings();
                outlinePen.DashStyle = updateStyle;
                outlinePen.Color = updateColor;
                shape.StyleSet.OverridePen(DiagramPens.ShapeOutline, outlinePen);
            }
            else
            {
                //Restore original outline style
                PenSettings settings = shape.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline);
                if (null != settings)
                {
                    if ((true == settings.IsOverridden(PenSettingsFlags.DashStyle))
                        || (true == settings.IsOverridden(PenSettingsFlags.Color)))
                        shape.StyleSet.ClearPenOverride(DiagramPens.ShapeOutline);
                }
            }
        }
        /// <summary>
        /// Updates the fill color of the shape
        /// </summary>
        public static void UpdateBackgroundFillColor(NodeShape shape, bool update, Color updateColor)
        {
            if (true == update)
            {
                //Change fill color
                BrushSettings fillBrush = new BrushSettings();
                fillBrush.Color = updateColor;
                shape.StyleSet.OverrideBrush(DiagramBrushes.ShapeBackground, fillBrush);
            }
            else
            {
                //Restore original color
                BrushSettings settings = shape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground);
                if (null != settings)
                {
                    if (true == settings.IsOverridden(BrushSettingsFlags.Color))
                        shape.StyleSet.ClearBrushOverride(DiagramBrushes.ShapeBackground);
                }
            }
        }
        /// <summary>
        /// Updates the text color of the shape
        /// </summary>
        public static void UpdateShapeTextStyle(NodeShape shape, bool update, Color updateColor)
        {
            if (true == update)
            {
                //Change fill color
                BrushSettings textBrush = new BrushSettings();
                textBrush.Color = updateColor;
                shape.StyleSet.OverrideBrush(DiagramBrushes.ShapeText, textBrush);
            }
            else
            {
                //Restore original color
                BrushSettings settings = shape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeText);
                if (null != settings)
                {
                    if (true == shape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeText).IsOverridden(BrushSettingsFlags.Color))
                        shape.StyleSet.ClearBrushOverride(DiagramBrushes.ShapeText);
                }
            }
        }
        #endregion
    }
}
